// pages/baby_file/baby_file.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 获取宝贝详细信息
   */
  getbobyinfo: function(e) {
    let that = this;
    let id = this.data.id
    wx.request({
      url: app.httpUrl + '/user/queryStudent/' + id,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: function(res) {
        console.log(res.data.data)
        wx.setStorageSync("babyInfo", res.data.data)
        that.setData({
          babyInfo: res.data.data
        }, () => {

          if (!res.data.data.studentNo || !res.data.data.studentSchoolName || !res.data.data.studentGrade) {
            wx.showModal({
              content: '请补充完整小孩信息',
              showCancel: false,
              success() {
                wx.navigateTo({
                  url: '/pages/addchild/addchild?types=1',
                })
              }
            })
          }else{
            that.getclass()
          }
        })
        wx.hideLoading()
      }
    })
  },
  /**
   * 转发获取用户token信息
   */
  gettoken: function() {
    let that = this;
    console.log(app.globalData.appUser != null)
    if (app.globalData.appUser != null) {
      return;
    }
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function(res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.getclass();
                that.getbobyinfo()
              }
            }
          })
        } else {
          // console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  /**
   * 获取课程列表
   */
  getclass: function() {
    let that = this;
    let childName;
    let parentId;
    // console.log(that.data)
    if (that.data.types == 1) { //如果是转发
      parentId = that.data.parentId
      childName = that.data.childName
    } else {
      parentId = app.globalData.appUser.userInfo.id;
      console.log(that.data.babyInfo)
      childName = that.data.babyInfo.studentName
    }
    // console.log(parentId)
    if (!childName) { //转发时候从转发链接里面获取的学生名字
      childName = that.data.studentName
    }
    if (!parentId) { //转发时候从转发链接里面获取的学生名字
      parentId = that.data.parentId
    }
    if (!app.globalData.openid) {
      app.globalData.openid = that.data.openid;
    }
    wx.request({
      url: app.httpUrl + '/activity/queryChildStudyAct',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        parentId: parentId,
        childName: childName,
      },
      success: function(res) {
        console.log(res.data.data)
        let all_time = 0; //累计学时
        for (let a = 0; a < res.data.data.length; a++) {
          //获取单节课的学习时间
          res.data.data[a].studyTime = res.data.data[a].endDate - res.data.data[a].startDate;
          res.data.data[a].startDate = util.formatDate(res.data.data[a].startDate, 'Y年M月D日');
          res.data.data[a].endDate = util.formatDate(res.data.data[a].endDate, 'Y年M月D日');
          res.data.data[a].studyTime = util.formatDate(res.data.data[a].studyTime, 'h');
          all_time += parseInt(res.data.data[a].studyTime)
        }
        that.setData({
          class_list: res.data.data,
          parentId: parentId
        })
        wx.hideLoading()
      }
    })
  },
  /**跳转活动中心 */
  activityLive: function(event) {
    var activityId = event.currentTarget.dataset.idx;
    var childId = event.currentTarget.dataset.childid;
    app.globalData.activityId = activityId;
    app.globalData.childId = childId;
    wx.switchTab({
      url: '/pages/study/study'
    })
  },
  /**
   * 跳转修改信息
   */
  jump_alter: function(e) {
    if (this.data.types == 1) {
      return
    } else {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/addchild/addchild?id=' + id,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    let id = options.id;
    // console.log(id)
    if (id == 'null') {
      wx.showModal({
        content: '请补充完整小孩信息',
        showCancel: false,
        success() {
          wx.navigateTo({
            url: '/pages/addchild/addchild?type=1',
          })
        }
      })
    }
    this.setData({
      id: id, //宝贝ID
      types: options.types,
      studentName: options.name, //学生名字
      parentId: options.parentId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getbobyinfo() //获取宝贝信息
    this.gettoken()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    let that = this;
    // console.log('pages/baby_file/baby_file?name=' + that.data.babyInfo.studentName + '&types=1' + '&parentId=' + app.globalData.appUser.userInfo.id + '&babyid=' + that.data.id)
    return {
      title: '',
      path: 'pages/baby_file/baby_file?name=' + that.data.babyInfo.studentName + '&types=1' + '&parentId=' + that.data.parentId + '&id=' + that.data.id
    }
  }
})